package com.momo.common.core.header;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.handlers.StrictFill;
import com.momo.common.core.request.SecurityContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

//@Slf4j
@Component
public class CustomizeMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
//        log.info("开始填充日期与登录用户");
        List<StrictFill<?, ?>> strictFillList = new ArrayList<>();
        strictFillList.add(StrictFill.of("createTime", LocalDateTime::now, LocalDateTime.class));
        strictFillList.add(StrictFill.of("updateTime", LocalDateTime::now, LocalDateTime.class));
        strictFillList.add(StrictFill.of("isDelete", () -> "1", String.class));
        strictFillList.add(StrictFill.of("createBy", () -> SecurityContext.getContext().getUsername(), String.class));
        strictFillList.add(StrictFill.of("updateBy", () -> SecurityContext.getContext().getUsername(), String.class));
        this.strictFill(true, this.findTableInfo(metaObject), metaObject, strictFillList);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateBy", () -> SecurityContext.getContext().getUsername(), String.class);
    }

}
